﻿using GeoBox.AzureWeb.Models;
using GeoBox.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace GeoBox.AzureWeb.Controllers.API
{
    public class GpsPointController : ApiController
    {
        private readonly GpsBoxEntities db = new GpsBoxEntities();

        public IEnumerable<GeoPoint> GetPoints()
        {
            var dev = db.Devices.Include("GpsPoints").FirstOrDefault(x => x.GpsPoints.Any());
            var date = dev.LastLocation.UtcTime.Date;
            var res = db.GpsPoints.Where(x => x.DeviceId == dev.Id && x.UtcTime >= date);
            return res.Select(x => new GeoPoint{ Latitude = x.Latitude, Longitude = x.Longitude, UtcTime = x.UtcTime }).ToList();
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
